#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, INF = 0x3f3f3f3f;

ll n, k;
ll a[N], ans[N];
bool vis[N];

ll quick_pow(ll a, ll b, ll p)
{
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % p;
        a = a * a % p, b >>= 1;
    }
    return res;
}

void solve()
{
    cin >> n >> k;
    for (ll i = 1;i <= n;i++)   cin >> a[i];

    vector<int> b;
    for (ll i = 1;i <= n;i++) {
        if (vis[i]) continue;
        b.clear();
        for (int p = i;vis[p] == 0;p = a[p]) { b.push_back(p);vis[p] = 1; }
        int len = quick_pow(2, k, b.size());
        for (int j = 0;j < b.size();j++)
            ans[b[j]] = b[(j + len) % b.size()];
    }
    for (int i = 1;i <= n;i++)
        cout << ans[i] << ' ';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}